/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/
 
/**
*\*\file main.c
*\*\author Nations
*\*\version v1.0.0
*\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
**/
#include <string.h>
#include "main.h"
#include "bsp_led.h"
#include "bsp_delay.h"
#include "usart.h"
#include "timer.h"
#include "pwm.h"
#include "key_input.h"
#include "flash.h"
#include "settings.h"
#include "log.h"
#include "i2c.h"
#include "message.h"

#ifdef ENABLE_WWDG_TEST
#include "wwdg.h"
#endif
/**
 *\*\name   main.
 *\*\fun    main function.
 *\*\param  none.
 *\*\return none.
**/
#define APPCATION_VERSION_DATE __DATE__
#define APPCATION_VERSION_TIME __TIME__

Led_ToggleFreq led_freq;
uint8_t isRunningUpdate = MI_FALSE;
uint16_t rxDataNum = 0;
uint8_t rxData[I2C_SEND_REC_DATA_LENGTH] = {0};
uint8_t txData[I2C_SEND_REC_DATA_LENGTH] = {0};
uint16_t txDataNum = 0;

int main(void)
{
	RCC_ClocksType RCC_Clocks;

  	led_freq = LED_TOGGLE_1000_MS;
	
	led_init();

	usart1_init();

	usart2_init();

	timer6_init();

	tim2_pwm_init();

	key_input_init();
	/* Delay 1s */
	SysTick_Delay_Ms(1000);

	print_text_message();

#ifdef ENABLE_WWDG_TEST
	/* Debug mode WWDG stopped */
	DBG_Peripheral_ON(DBG_WWDG_STOP);
	
	/* Check if the system has resumed from WWDG reset */
    if(RCC_Flag_Status_Get(RCC_FLAG_WWDGRST) != RESET)
	{
        printf("\r\n Reset By WWDG \r\n");
		
		/* Clear reset flags */
		RCC_Reset_Flag_Clear();
	}		
	else 
	{
		/* WWDG Reset flag is not set */
        /* Turn Off LED1 */
	}
#endif

	MI_CHAR name[24] = {0};
	MI_CHAR version[48] = {0};

	system_info_get_broad_name(name);
	system_info_get_app_version(version);

	// printf("from settings area read name  == %s\r\n",name);
	// printf("App Date == %s\r\n",APPCATION_VERSION_DATE);
	// printf("App Time == %s\r\n",APPCATION_VERSION_TIME);

	//RCC_Clocks_Frequencies_Value_Get(&RCC_Clocks);

    // printf("Pclk1Freq  == %d\r\n",RCC_Clocks.Pclk1Freq);
    // printf("Pclk2Freq  == %d\r\n",RCC_Clocks.Pclk2Freq);

    // printf("HclkFreq  == %d\r\n",RCC_Clocks.HclkFreq);
    // printf("SysclkFreq  == %d\r\n",RCC_Clocks.SysclkFreq);

	//i2c2_master_init();

#if I2C1_MODE == I2C1_SLAVE
	i2c1_slave_init();

	SysTick_Delay_Ms(5000);

#else
	MI_U8 w_read[5] = {0x50,0x60,0x70,0xf4,0x68};
	

	i2c1_master_init();
	i2c_master_send_bytes_by_addr(0xa0,0x00,w_read,5);
	SysTick_Delay_Ms(2);

	i2c_master_receive_bytes_by_addr(0xa0,0x00,r_data,5);
#endif

#ifdef ENABLE_WWDG_TEST
	/* Counter value to 16383, WWDG timeout = ~1024 us * 16320 = 16.7s
     In this case the refresh window is: ~1024 us * (16383 - 10000) = 6.6s < refresh window < ~1024 us * 16319 = 16.7s */
	WWDG_Config(WWDG_PRESCALER_DIV8, 0x2710, 0x3FFF);
	
	printf("\r\n Set the upper window value to 0x2710.\r\n");
#endif
	while(1)
	{
		// SysTick_Delay_Ms(50);
		// GPIO_Pin_Toggle(LED2_GPIO_PORT, LED1_GPIO_PIN);
				/* Insertion delay. delay 7s */
#ifdef ENABLE_WWDG_TEST				
		SysTick_Delay_Ms(7000);
		
		/* Feed the dog, reset the counter to the maximum value of 0x3FFF */
		WWDG_Feed(0x3FFF);
#endif		
	}
}


